<template>
  <div class="cart-container">
    <button class="cart-btn" @click="toggleCartPanel">
      <span class="cart-icon">🛒</span>
      <span class="cart-count">{{ cartItems.length }}</span>
    </button>
    
    <!-- 购物车面板 -->
    <div class="cart-panel" v-if="isCartOpen">
      <div class="cart-header">
        <h3 class="cart-title">购物车</h3>
        <button class="close-btn" @click="toggleCartPanel">×</button>
      </div>
      
      <div class="cart-content">
        <div v-if="cartItems.length === 0" class="empty-cart">
          <p>购物车是空的</p>
          <router-link to="/diy/ai" class="start-shopping-btn" @click="isCartOpen = false">
            开始定制花束
          </router-link>
        </div>
        
        <div v-else class="cart-items">
          <div v-for="(item, index) in cartItems" :key="index" class="cart-item">
            <div class="item-image">
              <img :src="item.image" :alt="item.name">
            </div>
            <div class="item-details">
              <h4 class="item-name">{{ item.name }}</h4>
              
              <!-- AI提示词 -->
              <div v-if="item.prompt" class="item-prompt">
                <p class="detail-title">AI提示词:</p>
                <p class="prompt-text">{{ item.prompt }}</p>
              </div>
            </div>
            
            <div class="item-actions">
              <button class="remove-btn" @click="removeItem(index)">删除</button>
            </div>
          </div>
        </div>
      </div>
      
      <div class="cart-footer" v-if="cartItems.length > 0">
        <button class="checkout-btn">结算</button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, onBeforeUnmount } from 'vue';

export default {
  name: 'CartComponent',
  setup() {
    // 购物车状态
    const isCartOpen = ref(false);
    const cartItems = ref([]);
    
    // 切换购物车面板显示状态
    const toggleCartPanel = () => {
      isCartOpen.value = !isCartOpen.value;
    };
    
    // 计算总价
    const calculateTotal = () => {
      return cartItems.value.reduce((total, item) => total + item.price, 0);
    };
    
    // 从购物车中移除商品
    const removeItem = (index) => {
      cartItems.value.splice(index, 1);
      // 保存到本地存储
      localStorage.setItem('cartItems', JSON.stringify(cartItems.value));
    };
    
    // 添加商品到购物车的事件处理函数
    const handleAddToCart = (bouquetData) => {
      cartItems.value.push(bouquetData);
      // 保存到本地存储
      localStorage.setItem('cartItems', JSON.stringify(cartItems.value));
      // 显示购物车面板
      isCartOpen.value = true;
    };
    
    // 点击页面其他区域关闭购物车面板
    const handleClickOutside = (event) => {
      const cartContainer = document.querySelector('.cart-container');
      if (isCartOpen.value && cartContainer && !cartContainer.contains(event.target)) {
        isCartOpen.value = false;
      }
    };
    
    // 组件挂载时
    onMounted(() => {
      // 从本地存储加载购物车数据
      const storedCart = localStorage.getItem('cartItems');
      if (storedCart) {
        cartItems.value = JSON.parse(storedCart);
      }
      
      // 监听添加到购物车事件
      window.addEventListener('click', handleClickOutside);
      
      // 使用全局事件总线监听添加到购物车事件
      window.addEventListener('add-to-cart', (event) => {
        if (event.detail) {
          handleAddToCart(event.detail);
        }
      });
    });
    
    // 组件卸载前
    onBeforeUnmount(() => {
      window.removeEventListener('click', handleClickOutside);
      window.removeEventListener('add-to-cart', handleAddToCart);
    });
    
    return {
      isCartOpen,
      cartItems,
      toggleCartPanel,
      calculateTotal,
      removeItem
    };
  }
};
</script>

<style scoped>
.cart-container {
  position: relative;
}

.cart-btn {
  background: none;
  border: none;
  position: relative;
  font-size: 1.5rem;
  cursor: pointer;
}

.cart-count {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: #e75480;
  color: white;
  font-size: 0.7rem;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cart-panel {
  position: absolute;
  top: 100%;
  right: 0;
  width: 350px;
  max-height: 80vh;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.cart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid #eee;
}

.cart-title {
  margin: 0;
  color: #333;
  font-size: 1.2rem;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #666;
}

.cart-content {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
}

.empty-cart {
  text-align: center;
  padding: 2rem 0;
  color: #666;
}

.start-shopping-btn {
  display: inline-block;
  margin-top: 1rem;
  padding: 0.5rem 1rem;
  background-color: #e75480;
  color: white;
  border-radius: 4px;
  text-decoration: none;
  transition: background-color 0.3s;
}

.start-shopping-btn:hover {
  background-color: #d64d7b;
}

.cart-items {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.cart-item {
  display: grid;
  grid-template-columns: 80px 1fr auto;
  gap: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #eee;
}

.item-image {
  width: 80px;
  height: 80px;
  border-radius: 4px;
  overflow: hidden;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-details {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.item-name {
  margin: 0;
  font-size: 1rem;
  color: #333;
}

.item-price {
  margin: 0;
  font-weight: bold;
  color: #e75480;
}

.item-type {
  margin: 0;
  font-size: 0.9rem;
  color: #666;
  background-color: #f5f5f5;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  display: inline-block;
}

.detail-title {
  margin: 0.5rem 0 0.2rem;
  font-size: 0.9rem;
  font-weight: bold;
  color: #666;
}

.flowers-list {
  margin: 0;
  padding-left: 1rem;
  font-size: 0.9rem;
  color: #666;
}

.card-message {
  font-style: italic;
  color: #666;
  font-size: 0.9rem;
}

.prompt-text {
  font-size: 0.9rem;
  color: #666;
  background-color: #f5f5f5;
  padding: 0.5rem;
  border-radius: 4px;
  margin: 0;
}

.item-actions {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.remove-btn {
  background: none;
  border: none;
  color: #999;
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0.3rem;
  transition: color 0.3s;
}

.remove-btn:hover {
  color: #e75480;
}

.cart-footer {
  padding: 1rem;
  border-top: 1px solid #eee;
  background-color: #f9f9f9;
}

.cart-total {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  font-size: 1.1rem;
}

.total-price {
  font-weight: bold;
  color: #e75480;
}

.checkout-btn {
  width: 100%;
  padding: 0.8rem;
  background-color: #e75480;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.3s;
}

.checkout-btn:hover {
  background-color: #d64d7b;
}

/* 响应式设计 */
@media (max-width: 576px) {
  .cart-panel {
    width: 100vw;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    max-height: 100vh;
    border-radius: 0;
  }
}
</style>